<%@ page import="com.ld.businessCard.service.CardService" %>
<%@ page import="com.ld.businessCard.service.impl.CardServiceImpl" %>
<%@ page import="com.ld.businessCard.entity.Card" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ld.businessCard.entity.User" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 16:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>名片管理</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        td {
            color: #ff4b2b;
            text-align: center;
        }
        form {
            width: 500px;
            height: 50px;
            margin: 0 auto;
        }
        input {
            width: 300px;
        }

        a {
            text-decoration: none;
            display: inline-block;
            text-align: center;
            color: black;
        }
        a:hover {
            color: blue;
        }
        .container {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 14px 28px rgba(0, 0, 0, 0.35), 0 10px 10px rgba(0, 0, 0, 0.22);
            position: relative;
            overflow: hidden;
            width: 80%;
            max-width: 100%;
            min-height: 480px;
            margin:0 auto;
        }
    </style>
</head>
<body>
<%!
    private CardService cardService = new CardServiceImpl();
%>
<%
    String name = request.getParameter("name");
    List<Card> cards;
	if(name == null){
      cards = cardService.getAllCards();
    }else {
        cards = cardService.getCardsByLikeName(name,null);
    }
    User user = (User)(session.getAttribute("user"));
    String homePath;
    if(user.getStatus() == 0){
        homePath = "user";
    }else {
        homePath = "admin";
    }
    out.println("<a href='/home/"+homePath + "/index.jsp'" + "  style='display: inline-block;width:30%,height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:blue'>返回个人管理页面</a><br>");
	out.println("<form action='/home/admin/card.jsp' method='get'><input name='name'>&nbsp&nbsp<input type='submit' value='按姓名模糊查询'></form>");
    out.println("<a href='/home/admin/addCard.jsp' style='display: inline-block;width:30%,height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:orange'>添加名片</a>");
    out.println("<a href='/card/downLoad' style='display: inline-block;width:30%,height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:orange'>导出为excel</a>");
    out.println("<a href='/card/template' style='display: inline-block;width:30%,height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:orange'>下载导入excel模板</a>");
    out.println("<a href='/home/user/upLoadExcel.jsp' style='display: inline-block;width:30%,height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:orange'>导入excel</a>");
    if (cards.isEmpty()) out.println("<h1 style='width: 80%;height:400px;margin: 0 auto;text-align:center;line-height:400px'>暂无更多信息!<h1>");
    else {
        out.println("<table class='container' >" +
                "<tr>" +
                "<th>姓名</th>" +
                "<th>性别</th>" +
                "<th>邮箱</th>" +
                "<th>用户ID<th>" +
                "<th>操作</th>" +
                "</tr>");
        if (!cards.isEmpty())
            for (Card card : cards) {
                int sex0 = card.getSex();
                String sex;
                if (sex0 == 0) {
                    sex = "男";
                } else {
                    sex = "女";
                }
                String changeDOM = "<td><a href=" + "'/home/card/changeMyCard.jsp?id=" + card.getId() + "'>修改" + "</a></td>";
                String deleteDOM = "<td><a  class='recyle' href=" + "'/card/recycle?id=" + card.getId() + "'>回收站" + "</a></td>";
                String deletedDOM = "<td ><a class='deleted' href=" + "'/card/delete?id=" + card.getId() + "'>永久删除" + "</a></td>";
                out.println("<tr>");
                out.println("<td>" + card.getName() + "</td>");
                out.println("<td>" + sex + "</td>");
                out.println("<td>" + card.getEmail() + "</td>");
                out.println("<td>" + card.getUserId() + "</td>");
                out.println(changeDOM);
                out.println(deleteDOM);
                out.println(deletedDOM);
                out.println("</tr>");
            }
        out.println("</table>");
    }

%>
</body>
<script>
    let deleted = document.getElementsByClassName("deleted");
    for (let i = 0; i < deleted.length; i++) {
        deleted[i].addEventListener("click",(e) => {
            let b = confirm("你确定要彻底删除吗?");
            if(b == false){
                e.preventDefault();
            }
        })
    }
    let recyle = document.getElementsByClassName("recyle");
    for (let i = 0; i < recyle.length; i++) {
        recyle[i].addEventListener("click",(e) => {
            let b = confirm("你确定要放入回收站吗?");
            if(b == false){
                e.preventDefault();
            }
        })
    }
</script>
</html>
